package com.apestech.framework.xml.plugin.node;

import com.apestech.framework.xml.pub.core.ElementUtil;
import com.apestech.framework.xml.plugin.modul.Database;
import org.dom4j.Element;

import java.util.List;

/**
 * @author hup
 * @create 2018-12-10-10:24
 */
public class NodeDatabase extends ElementUtil{
    private Element node;
    private Database db=new Database();

    public NodeDatabase(Element node){
        this.node=node;
    }

    public Database getDb() {
        return db;
    }

    public void getAllDb() throws Exception {
        List<?> list=node.content();
        int len=list.size();
        for(int i=0; i<len; i++){
            String scalss=list.get(i).getClass().getName();
            if(scalss.equals("org.dom4j.tree.DefaultElement")){
                getDbList((Element) list.get(i));
            }
        }
    }

    private void getDbList(Element e) throws Exception {
        String theme=e.getName();
        List<?> list=e.content();
        int len=list.size();
        for(int i=0; i<len; i++) {
            String scalss = list.get(i).getClass().getName();
            if (scalss.equals("org.dom4j.tree.DefaultElement")) {
                Element eml = (Element) list.get(i);
                db.addDB(theme+"."+eml.getName(), getNodeAttr(eml).getJSONObject("attrNode"));
            }
        }
    }

}
